#include "root.h"

typedef struct {
	unsigned long magic;
	unsigned short crc;
	unsigned short len;

	unsigned char *pdata;
	unsigned long size;
	unsigned long prog_addr;
	unsigned long prog_max;

	void (*print)(unsigned char level, char *format, ...);
	void (*sleep)(unsigned long ticks);
} proto_calls_t;

#define FLASHADDR_IAPFUNC	0x1F000

const UCHAR iap_tab[] = {
	0xf0, 0xb5, 0x83, 0xb0, 0x04, 0x00, 0x5d, 0xd0, 0x2f, 0x48, 0x30, 0x49, 0x87, 0x08, 0x2f, 0x48,
	0xbf, 0x00, 0x84, 0x46, 0x2e, 0x48, 0x86, 0x46, 0x2e, 0x48, 0x40, 0x1a, 0xc0, 0x1c, 0x85, 0x08,
	0xad, 0x00, 0x6e, 0x1e, 0x00, 0x2e, 0x16, 0xdd, 0x62, 0x46, 0x01, 0x23, 0x79, 0x1e, 0xe8, 0x07,
	0xdb, 0x07, 0x52, 0x1e, 0xd8, 0x42, 0x03, 0xd0, 0x48, 0x78, 0x50, 0x70, 0x49, 0x1c, 0x52, 0x1c,
	0x48, 0x78, 0x73, 0x10, 0x07, 0xd0, 0x8e, 0x78, 0x50, 0x70, 0xc8, 0x78, 0x96, 0x70, 0x89, 0x1c,
	0x92, 0x1c, 0x5b, 0x1e, 0xf7, 0xd1, 0x6d, 0x1e, 0x02, 0xd4, 0x79, 0x5d, 0x60, 0x46, 0x41, 0x55,
	0x1d, 0x48, 0x1b, 0x49, 0x40, 0x1a, 0xc0, 0x1c, 0x81, 0x08, 0x89, 0x00, 0x00, 0x29, 0x0e, 0xdd,
	0x70, 0x46, 0xcb, 0x07, 0x00, 0x22, 0x40, 0x1e, 0x00, 0x2b, 0x01, 0xd0, 0x42, 0x70, 0x40, 0x1c,
	0x49, 0x10, 0x04, 0xd0, 0x42, 0x70, 0x82, 0x70, 0x80, 0x1c, 0x49, 0x1e, 0xfa, 0xd1, 0x13, 0x4d,
	0xa6, 0x69, 0x2e, 0x60, 0xe0, 0x69, 0x68, 0x60, 0x11, 0x4a, 0x12, 0x49, 0x12, 0x48, 0x12, 0x78,
	0x09, 0x78, 0x00, 0x78, 0x00, 0x90, 0x11, 0x48, 0x02, 0x92, 0x01, 0x91, 0x03, 0x78, 0x10, 0x48,
	0x10, 0xa1, 0x02, 0x78, 0x00, 0x20, 0xb0, 0x47, 0x69, 0x68, 0x0a, 0x20, 0x88, 0x47, 0x20, 0x46,
	0x00, 0xf0, 0x30, 0xf8, 0x03, 0xb0, 0xf0, 0xbd, 0xc7, 0xf2, 0x01, 0x00, 0x00, 0x1f, 0x00, 0x20,
	0x08, 0x1f, 0x00, 0x20, 0x08, 0x1f, 0x00, 0x20, 0x08, 0x1f, 0x00, 0x20, 0x00, 0x1f, 0x00, 0x20,
	0xc0, 0xf2, 0x01, 0x00, 0xbf, 0xf2, 0x01, 0x00, 0xbe, 0xf2, 0x01, 0x00, 0xbd, 0xf2, 0x01, 0x00,
	0xbc, 0xf2, 0x01, 0x00, 0x48, 0x54, 0x36, 0x58, 0x31, 0x58, 0x2d, 0x49, 0x41, 0x50, 0x20, 0x52,
	0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x32, 0x30, 0x25,
	0x30, 0x32, 0x64, 0x2d, 0x25, 0x64, 0x2d, 0x25, 0x64, 0x20, 0x25, 0x64, 0x3a, 0x25, 0x64, 0x0a,
	0x00, 0x00, 0x00, 0x00, 0x70, 0xb5, 0x05, 0x46, 0xc0, 0x68, 0x16, 0x49, 0x41, 0x18, 0x8e, 0x0a,
	0x69, 0x69, 0x88, 0x42, 0x25, 0xd2, 0x72, 0xb6, 0x00, 0x24, 0x00, 0x2e, 0x07, 0xd9, 0x28, 0x69,
	0xa1, 0x02, 0x40, 0x18, 0x00, 0xf0, 0x48, 0xf8, 0x64, 0x1c, 0xb4, 0x42, 0xf7, 0xd3, 0xec, 0x68,
	0xa0, 0x07, 0x02, 0xd0, 0xe4, 0x1c, 0xa4, 0x07, 0xa4, 0x0f, 0xae, 0x68, 0x00, 0x2c, 0x08, 0xd0,
	0x02, 0xce, 0x28, 0x69, 0x00, 0xf0, 0x5e, 0xf8, 0x28, 0x69, 0x00, 0x1d, 0x28, 0x61, 0x24, 0x1f,
	0xf6, 0xd1, 0xbf, 0xf3, 0x4f, 0x8f, 0x05, 0x49, 0x03, 0x48, 0xc8, 0x60, 0xbf, 0xf3, 0x4f, 0x8f,
	0xfe, 0xe7, 0x70, 0xbd, 0xff, 0x03, 0x00, 0x00, 0x04, 0x00, 0xfa, 0x05, 0x00, 0xed, 0x00, 0xe0,
	0x43, 0x49, 0x42, 0x48, 0x08, 0x60, 0x70, 0x47, 0x41, 0x49, 0x00, 0x20, 0x08, 0x60, 0x70, 0x47,
	0x3f, 0x48, 0x00, 0x68, 0xc0, 0x07, 0x00, 0xd0, 0x01, 0x20, 0x70, 0x47, 0x3c, 0x49, 0x3d, 0x48,
	0x88, 0x63, 0x70, 0x47, 0x3a, 0x49, 0x00, 0x20, 0x88, 0x63, 0x70, 0x47, 0x3b, 0x49, 0x3a, 0x48,
	0x08, 0x61, 0x70, 0x47, 0x39, 0x49, 0x00, 0x20, 0x08, 0x61, 0x70, 0x47, 0x34, 0x48, 0x80, 0x6b,
	0xc0, 0x07, 0x00, 0xd0, 0x01, 0x20, 0x70, 0x47, 0x01, 0x21, 0x49, 0x04, 0x88, 0x42, 0x01, 0xd3,
	0x02, 0x20, 0x70, 0x47, 0x2e, 0x49, 0x2d, 0x4a, 0x0a, 0x60, 0x2e, 0x4a, 0x8a, 0x63, 0x02, 0x22,
	0x4a, 0x63, 0x00, 0x22, 0x02, 0x60, 0x48, 0x6b, 0x40, 0x07, 0xfc, 0xd4, 0x8a, 0x63, 0x0a, 0x60,
	0x00, 0x20, 0x70, 0x47, 0x26, 0x48, 0x25, 0x49, 0x01, 0x60, 0x26, 0x49, 0x81, 0x63, 0x03, 0x21,
	0x41, 0x63, 0x00, 0x22, 0x12, 0x60, 0x41, 0x6b, 0x49, 0x07, 0xfc, 0xd4, 0x82, 0x63, 0x02, 0x60,
	0x00, 0x20, 0x70, 0x47, 0x22, 0x4a, 0x90, 0x42, 0x01, 0xd9, 0x02, 0x20, 0x70, 0x47, 0x1c, 0x4a,
	0x1a, 0x4b, 0x13, 0x60, 0x1b, 0x4b, 0x93, 0x63, 0x01, 0x23, 0x53, 0x63, 0x01, 0x60, 0x50, 0x6b,
	0x40, 0x07, 0xfc, 0xd4, 0x00, 0x20, 0x90, 0x63, 0x10, 0x60, 0x70, 0x47, 0x19, 0x4a, 0x90, 0x42,
	0x01, 0xd9, 0x02, 0x20, 0x70, 0x47, 0x12, 0x4a, 0x10, 0x4b, 0x13, 0x60, 0x11, 0x4b, 0x93, 0x63,
	0x01, 0x23, 0x53, 0x63, 0x01, 0x80, 0x50, 0x6b, 0x40, 0x07, 0xfc, 0xd4, 0x00, 0x20, 0x90, 0x63,
	0x10, 0x60, 0x70, 0x47, 0x10, 0x4a, 0x90, 0x42, 0x01, 0xd9, 0x02, 0x20, 0x70, 0x47, 0x08, 0x4a,
	0x06, 0x4b, 0x13, 0x60, 0x07, 0x4b, 0x93, 0x63, 0x01, 0x23, 0x53, 0x63, 0x01, 0x70, 0x50, 0x6b,
	0x40, 0x07, 0xfc, 0xd4, 0x00, 0x20, 0x90, 0x63, 0x10, 0x60, 0x70, 0x47, 0x5a, 0xa5, 0x00, 0x00,
	0x00, 0xf0, 0x00, 0x40, 0x68, 0x7a, 0x00, 0x00, 0x98, 0xf9, 0x00, 0x00, 0x40, 0xf0, 0x00, 0x40,
	0xfc, 0xff, 0x01, 0x00, 0xfe, 0xff, 0x01, 0x00, 0xff, 0xff, 0x01, 0x00, 0x10, 0x05, 0x12, 0x0b,
	0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

typedef void (*pfcallfunc_t)(proto_calls_t *pcall);

static int check_iapcrc(void)
{	
	UCHAR *pDest;
	USHORT fshcrc, tabcrc;
	ULONG size = ARRAY_SIZE(iap_tab);
	
	pDest = (UCHAR *)FLASHADDR_IAPFUNC;
	fshcrc = cal_crc(pDest, size);

	pDest = (UCHAR *)iap_tab;
	tabcrc = cal_crc(pDest, size);
	
	return (fshcrc != tabcrc) ? 1 : 0;	
}

static void write_iapfunc(void)
{
	ULONG addr = FLASHADDR_IAPFUNC;
	UCHAR *val = (UCHAR *)iap_tab;
	int size = ARRAY_SIZE(iap_tab);	
	int i;
	
	__disable_irq();

	for(i = 0; i < 4; i++)
	{
		FLASH_ErasePage(addr + i * FLASH_PAGE_SIZE);		
	}	

	if(0 != (size & 0x03))
	{
		size = (size + 3) % 4;
	}

	WPREG_Disable();
  FLASH_Unlock();

	while(size)
	{		
		FLASH_Program_M8(addr, *val++);
		addr ++;
		size --;
	}	

	FLASH_Lock();
     	WPREG_Enable();
		
	__enable_irq();
}

int load_iapfunc(UCHAR *pdata, ULONG size, ULONG prog_addr, ULONG prog_max)
{
	UCHAR *pflash;
	pfcallfunc_t pfstart;
	proto_calls_t calls;	

	if(check_iapcrc())
	{
		print_logo(0, "Start program iap...\n");
		
		os_sleep(10);		
		write_iapfunc();
		os_sleep(10);
	
		if(check_iapcrc())
		{
			print_logo(0, "Program iap failed!!!\n");
			return 1;
		}			
	}	

	calls.pdata = pdata;
	calls.size = size;
	calls.prog_addr = prog_addr;
	calls.prog_max = prog_max;
	calls.print = print_logo;
	calls.sleep = os_sleep;
	
	pflash = (UCHAR *)(FLASHADDR_IAPFUNC+1);
	pfstart = (pfcallfunc_t)pflash;		
	pfstart(&calls);

	return 0;
}

